<?php
class Country_Widget_ProfileCountriesJoinedController extends Engine_Content_Widget_Abstract
{
  protected $_childCount;
  
  public function indexAction()
  {
    // Don't render this if not authorized
    $viewer = Engine_Api::_()->user()->getViewer();
	if( !$viewer->getIdentity() ) {
      return $this->setNoRender();
    }
	
    // Get paginator
	$countryTable  = Engine_Api::_()->getDbtable('countries','country');
	$countryName = $countryTable->info('name');
	$memberTable = Engine_Api::_()->getDbtable('countrymembers','country');
	$memberName = $memberTable->info('name');
	
	$select = $countryTable->select()
				   ->setIntegrityCheck(false)  
				   ->from($memberName) 
				   ->where('user_id = ?', $viewer->getIdentity())
				   ->join($countryName,"$countryName.country_id = $memberName.country_id");   
    $this->view->paginator = $paginator = Zend_Paginator::factory($select);

    // Set item count per page and current page number
    $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 60));
    $paginator->setCurrentPageNumber($this->_getParam('page', 1));

    // Do not render if nothing to show
    if( $paginator->getTotalItemCount() <= 0 ) {
      return $this->setNoRender();
    }
  }

  public function getChildCount()
  {
    return $this->_childCount;
  }
}